package com.wwz.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.AsyncRabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/*@Component*/
/*
@Slf4j
*/
public class RabbitSender {

    private  Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(Long orderId,final long delayTimes){
        //给延迟队列发送消息
        rabbitTemplate.convertAndSend(RabbitMqConfig.QueueEnum.SIGNLE.getExchangeName(),RabbitMqConfig.QueueEnum.SIGNLE.getBindKeyName(), orderId, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                MessageProperties messageProperties = message.getMessageProperties();
                messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                messageProperties.setExpiration(String.valueOf(delayTimes));
                //给消息设置延迟毫秒值
                messageProperties.setDelay(500);
                return message;
            }
        });
        LOGGER.info("send orderId:{}",orderId);
    }
}
